package gui.screens;

import gui.CanvasManager;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import application.MainController;
import domain.Game;

/**
 * This class shows the list of games a hunter can join.
 * @author Sandra Mayer
 *
 */
public class ListOfGamesScreen extends Canvas implements CommandListener {
	
	private static final String FILENAME = "/mainWhite.jpg";
	private static final String CURSORNAME = "/arrowForMenu.png";

	private Image backgroundImg;
	private Image cursorImg;
	private int nrOfItems;
	private int[][] posOfCursor;
	
	private int pointerPos = 0;
	private Command exitCmd;
	private Command okCmd;
	private Command refreshCmd;
	
	private Vector /*Game*/ games;
	
	/**
	 * Constructor.
	 * @throws IOException IOException
	 */
	public ListOfGamesScreen() throws IOException {
		CanvasManager.setCurScreen(this);
		backgroundImg = Image.createImage(FILENAME);
		cursorImg = Image.createImage(CURSORNAME);

		exitCmd = new Command("Exit", Command.EXIT, 1);
		okCmd = new Command("OK", Command.OK, 1);
		refreshCmd = new Command("Refresh", Command.OK, 1);
		
		this.addCommand(exitCmd);
		this.addCommand(okCmd);
		this.addCommand(refreshCmd);
		this.setCommandListener(this);

		games = MainController.getInstance().getGameList();
		if (games != null) 
			nrOfItems = games.size();
		if (nrOfItems > 0) {
			posOfCursor = new int[nrOfItems][2];
			posOfCursor[0][0] = 15;
			posOfCursor[0][1] = 88;
		} else {
			posOfCursor = new int[1][2];
			posOfCursor[0][0] = 15;
			posOfCursor[0][1] = 88;
		}
	}
	
	/**
	 * Paints the screen.
	 */
	protected void paint(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.translate((getWidth() - backgroundImg.getWidth()) / 2,
				(getHeight() - backgroundImg.getHeight()) / 2);

		g.drawImage(backgroundImg, 0, 0, Graphics.TOP | Graphics.LEFT);

		g.drawImage(cursorImg, posOfCursor[pointerPos][0],
				posOfCursor[pointerPos][1], Graphics.HCENTER
						| Graphics.VCENTER);
		
		g.setColor(0x000000);
		
		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
				Font.SIZE_MEDIUM));
		
		g.drawString("ID", 25, 50, Graphics.LEFT | Graphics.TOP);
		g.drawString("Name", 45, 50, Graphics.LEFT | Graphics.TOP);
		g.drawString("Available", 165, 50, Graphics.LEFT | Graphics.TOP);
		
		int x = 25;
		int y = 80;
		
		if (games != null) {

			for (int i = 0; i < nrOfItems; i++) {
				posOfCursor[i][0] = x - 10;
				posOfCursor[i][1] = y + 8;
				Game game = (Game)games.elementAt(i);
				if(MainController.getInstance().isAvailable(game)) {
					g.setColor(0x000000);
				} else {
					g.setColor(0x838383);
				}

				g.drawString(game.getGameID() + "", x, y, Graphics.LEFT | Graphics.TOP);
				x += 20;
				g.drawString(game.getName(), x, y, Graphics.LEFT | Graphics.TOP);
				x += 165;				
				g.drawString((MainController.getInstance().isAvailable(game) ? "yes" : "no"), x, y, Graphics.LEFT | Graphics.TOP);
				x = 25;
				y += 25;
			}
		}
	}
	
	/**
	 * Checks which key was pressed and does the action for it.
	 */
	protected void keyPressed(int keyCode) {
		switch (getGameAction(keyCode)) {
		case UP:
			pointerPos = --pointerPos < 0 ? nrOfItems - 1 : pointerPos;
			break;
		case DOWN:
			pointerPos = ++pointerPos >= nrOfItems ? 0 : pointerPos;
			break;
		case FIRE:
			connect();
			break;
		default:
			switch (keyCode) {
			case KEY_NUM2:
				pointerPos = --pointerPos < 0 ? nrOfItems - 1 : pointerPos;
				break;
			case KEY_NUM8:
				pointerPos = ++pointerPos >= nrOfItems ? 0 : pointerPos;
				break;
			case KEY_NUM5:
				connect();
				break;
			}
		}
		repaint();
	}
	
	/**
	 * Connects the client to a game.
	 *
	 */
	private void connect() {
		if (games != null) {
			if((MainController.getInstance().isAvailable((Game)games.elementAt(pointerPos)))) {
				if (MainController.getInstance().connectToGame((Game)games.elementAt(pointerPos))) {
					CanvasManager.setCurrent(new RBCanvas());
				} else {
					CanvasManager.setCurrent(new LoginScreen((Game)games.elementAt(pointerPos)));
				}
			}
		}
		
	}

	/**
	 * Does the command action.
	 */
	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == okCmd) {
			keyPressed(getKeyCode(FIRE));
		} else if (cmd == exitCmd) {
			CanvasManager.showExitScreen();
		} else if (cmd == refreshCmd) {
			games = MainController.getInstance().getGameList();
			if (games != null) 
				nrOfItems = games.size();
			if (nrOfItems > 0) {
				posOfCursor = new int[nrOfItems][2];
				posOfCursor[0][0] = 15;
				posOfCursor[0][1] = 88;
			} else {
				posOfCursor = new int[1][2];
				posOfCursor[0][0] = 15;
				posOfCursor[0][1] = 88;
			}
			pointerPos = 0;
			repaint();
		}
	}

}
